package com.springboot.service.impl;

import com.springboot.dto.Doctor;
import com.springboot.dto.Doctor2;
import com.springboot.mapper.AuthenticationMapper;
import com.springboot.mapper.DoctorFieldFormMapper;
import com.springboot.mapper.DoctorInfoFormMapper;
import com.springboot.mapper.OrderFormMapper;
import com.springboot.model.Authentication;
import com.springboot.model.DoctorFieldForm;
import com.springboot.model.DoctorInfoForm;
import com.springboot.model.OrderForm;
import com.springboot.service.PatientRepeatOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PatientRepeatOrderServiceImpl implements PatientRepeatOrderService {
    @Autowired
    OrderFormMapper orderFormMapper;

    @Autowired
    DoctorInfoFormMapper doctorInfoFormMapper;

    @Autowired
    DoctorFieldFormMapper doctorFieldFormMapper;

    @Autowired
    AuthenticationMapper authenticationMapper;

    @Override
    public ArrayList<Doctor2> findOrderedDoctorsByPatientId(Integer patientId) {
        OrderForm orderForm = new OrderForm();
        List<Integer> list = new ArrayList<>();
        try {
            list = orderFormMapper.selectDoctorIdByPatientId(patientId);
        }
        catch (Exception e)
        {
            System.out.println("PatientRepeatOrderServiceImpl错误");
            System.out.println("list = orderFormMapper.selectDoctorIdByPatientId(patientId)错误");
        }
        ArrayList<Doctor2> doctor2s = new ArrayList<>();
        for(int i=0;i<list.size();i++)
        {
            Integer doctorId = list.get(i);
            try {
                DoctorInfoForm doctorInfoForm = doctorInfoFormMapper.selectByPrimaryKey(doctorId);
                DoctorFieldForm doctorFieldForm = doctorFieldFormMapper.selectDoctorFieldById(doctorId);
                Authentication authentication = authenticationMapper.selectByPrimaryKey(doctorId);
                Doctor2 doctor2 = new Doctor2();
                doctor2.setDoctorInfoForm(doctorInfoForm);
                doctor2.setDoctorFieldForm(doctorFieldForm);
                doctor2.setDoctorPhoto(authentication.getHeadLink());
                doctor2s.add(doctor2);
            }
            catch (Exception e)
            {
                System.out.println("findOrderedDoctorsByPatientId错误");
                System.out.println("doctor2视图构造错误：doctorId="+doctorId);
            }
        }
        return doctor2s;
    }
}
